/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controladores;

import Beans.Proveedor;
import Beans.Venta;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author CRISTIAN_2
 */
public class Mventa {

File archivo=new File("D:\\persistencia\\Ventas.dat");

public void grabar() throws IOException {

      FileOutputStream   flujoSalida  = new FileOutputStream(archivo);
      ObjectOutputStream objetoSalida = new ObjectOutputStream(flujoSalida);
      objetoSalida.writeObject(this.ventas);
      objetoSalida.close();
     }

public void restaurar() throws IOException,ClassNotFoundException {

 	FileInputStream flujoEntrada    = new FileInputStream(archivo);
  	ObjectInputStream objetoEntrada = new ObjectInputStream(flujoEntrada);
  	List<Venta> vent = (List) objetoEntrada.readObject();
  	this.setVentas(vent);
        objetoEntrada.close();
    }




    private List <Venta> ventas = new LinkedList<Venta>();//lista
    Venta va= new Venta();

    public List<Venta> getVentas() {
        return ventas;
    }
    

    public void setVentas(List<Venta> ventas) {
        this.ventas = ventas;
    }

    public Boolean Existe(int cod){
        boolean sw=false;
        Iterator it = ventas.iterator();
        while(it.hasNext()){
            va = (Venta)it.next();
            if(va.getCodigo()==cod){
                sw=true;
            }
        }
        return sw;
    }


      public void Modificar(int pos,int cod,Venta ven){
       Iterator itc=ventas.iterator();
         while(itc.hasNext()){
             va = (Venta)itc.next();
            if(va.getCodigo() == cod){
                ventas.set(pos, ven);
                JOptionPane.showMessageDialog(null,"Los datos se guardaron "
                                                 + "Satisfactoriamente");
            }
        }
       }
     public void Registrar(Venta ve){
         ventas.add(ve);
      }

    public Venta Consultar(int cod){
        Iterator it = ventas.iterator();
        while(it.hasNext()){
            Venta vat = (Venta)it.next();
              if(vat.getCodigo() == cod){
                 return vat;
              }else{
                 JOptionPane.showMessageDialog(null,"Venta  no encontrado4");
              }
         }
        return new Venta();
     }

     public Venta Recorrer(){
         Iterator it = ventas.iterator();
         while(it.hasNext()){
             Venta vat = (Venta)it.next();
             return vat;
         }
         return new Venta();
     }

     public void Eliminar(int cod){
        Iterator it = ventas.iterator();
         while(it.hasNext()){
            va =(Venta)it.next();
            if(va.getCodigo()== cod){
                 ventas.remove(va);
                  JOptionPane.showMessageDialog(null,"Eliminado");
             }
         }
     }




}
